package customerEnter.Frame.ehcache.AvailableEhcache;

import net.sf.ehcache.Element;
import net.sf.json.JSONArray;
import customerEnter.Frame.ehcache.Judge.MapEcacheSqlQUery;
import customerEnter.Frame.ehcache.Judge.MapEcacheTrueOrFalse;
import customerEnter.Frame.ehcache.Judge.MapEcacheUpdateTime;
import customerEnter.tool.WhetherToEmpty;

/**
 * 設置參數數據
 * 
 * 类名称：EcacheParameterGet 类描述： 创建人：dy 创建时间：2016-4-29 下午5:16:37
 * 
 */
@SuppressWarnings("static-access")
public class EcacheParameterSet {
	private static EcacheParameterSet	ecacheParameterSet;

	private EcacheParameterSet() {
	}

	public static EcacheParameterSet getEcacheParameterSet() {
		if (WhetherToEmpty.ObjectWhetherToEmpty(ecacheParameterSet)) {
			ecacheParameterSet = new EcacheParameterSet();
		}
		return ecacheParameterSet;
	}

	/**
	 * 添加到緩存
	 */
	public void addEach(Object key, Object value) {
		Element element = null;
		if (EcacheDataTransmission.getEcacheDataTransmission().GetEach().get(key) == null) {
			element = new Element(key, value);
		} else {
			element = EcacheDataTransmission.getEcacheDataTransmission().GetEach().get(key);
		}
		EcacheDataTransmission.getEcacheDataTransmission().GetEach().put(element);
		if (!WhetherToEmpty.ObjectWhetherToEmpty(value)) {
			Element element2 = EcacheDataTransmission.getEcacheDataTransmission().GetEach().get(key);
			if (WhetherToEmpty.ObjectWhetherToEmpty(element2)) {
				System.out.println(key + ":" + "添加不成功");
			}
		}
	}

	/**
	 * 添加到緩存 限制時間 默認一天 0表示一天
	 */
	public void addEachTime(Object key, Object value, Integer TimeMos) {
		Element element = null;
		if (EcacheDataTransmission.getEcacheDataTransmission().GetEach().get(key) == null) {
			element = new Element(key, value);
			if (TimeMos <= 0) {
				element.setTimeToLive(1000 * 60 * 60 * 24);
			} else {
				element.setTimeToLive(TimeMos);
			}
		} else {
			element = EcacheDataTransmission.getEcacheDataTransmission().GetEach().get(key);
		}
		EcacheDataTransmission.getEcacheDataTransmission().GetEach().put(element);
		if (!WhetherToEmpty.ObjectWhetherToEmpty(value)) {
			Element element2 = EcacheDataTransmission.getEcacheDataTransmission().GetEach().get(key);
			if (WhetherToEmpty.ObjectWhetherToEmpty(element2)) {
				System.out.println(key + ":" + "添加不成功");
			}
		}
	}

	/**
	 * 設置數據緩存真實的匹配
	 */

	private void setDataIf(String key, Boolean bool) {
		MapEcacheTrueOrFalse.getMapEcacheTrueOrFalse().SetDataIf(key, bool);
	}

	/**
	 * 設置sql
	 */
	private void setSqlQuery(String key, String Sql) {
		MapEcacheSqlQUery.getMapEcacheSqlQUery().SetDataIf(key, Sql);
	}

	/**
	 * 設置更新緩存時間
	 */
	private void setEachUpdateTime(String key, String time) {
		MapEcacheUpdateTime.getMapEcacheUpdateTime().SetDataIf(key, time);
	}

	/**
	 * 把參數添加到緩存系統
	 * 
	 * @param key
	 *            緩存的key
	 * @param jsonArray
	 *            緩存的數據
	 * @param bool
	 *            緩存的驗證
	 * @param Sql
	 *            緩存的sql語句
	 * @param time
	 *            緩存更新的時間
	 */
	public void setEachParameter(String key, JSONArray jsonArray, Boolean bool, String Sql, String time) {
		getEcacheParameterSet().setDataIf(key, bool);
		getEcacheParameterSet().setEachUpdateTime(key, time);
		getEcacheParameterSet().setSqlQuery(key, Sql);
		getEcacheParameterSet().addEach(key, jsonArray);
	}

}
